package com.css.fxfzypg.modules.rating.library.dao;

import com.css.fxfzypg.modules.rating.library.model.entity.KpExamScoringDbIndex1;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

@Slf4j
@Repository
public class KpExamScoringDbIndex1Repository {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    public void batchSave(List<KpExamScoringDbIndex1> index1List) {
        String sql = "INSERT INTO kp_exam_scoring_db_index1(id, name, kesd_id, create_time) values(:id, :name, :kesdId, :createTime)";
        namedParameterJdbcTemplate.batchUpdate(sql, SqlParameterSourceUtils.createBatch(index1List.toArray()));
    }

    public void updateById(KpExamScoringDbIndex1 obj) {
        StringBuilder sql = new StringBuilder("update kp_exam_scoring_db_index1 set ");
        if (ObjectUtils.isNotEmpty(obj.getName())) {
            sql.append(" name = '").append(obj.getName()).append("', ");
        }
//        if (ObjectUtils.isNotEmpty(obj.getScore())) {
//            sql.append(" score = '").append(obj.getScore()).append("', ");
//        }
        sql.append(" where id = '").append(obj.getId()).append("'");
        sql.deleteCharAt(sql.lastIndexOf(", "));
        jdbcTemplate.update(sql.toString());
    }

    public void insert(KpExamScoringDbIndex1 kpExamScoringDbIndex1) {
//        String sql = "INSERT INTO kp_exam_scoring_db_index1(id, name, score, kesd_id, create_time) values(:id, :name, :score, :kesdId, :createTime)";
        String sql = "INSERT INTO kp_exam_scoring_db_index1(id, name, kesd_id, create_time) values(:id, :name, :kesdId, :createTime)";
        namedParameterJdbcTemplate.update(sql, new BeanPropertySqlParameterSource(kpExamScoringDbIndex1));
    }

    public KpExamScoringDbIndex1 getById(String id) {
        try {
            if (id == null) {
                return null;
            }
            String sql = "select * from kp_exam_scoring_db_index1 where id = '" + id + "'";
            return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(KpExamScoringDbIndex1.class));
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    public void deleteById(String id) {

    }

    public List<KpExamScoringDbIndex1> list(KpExamScoringDbIndex1 kpExamScoringDbIndex1) {
        StringBuilder sql = new StringBuilder("select * from kp_exam_scoring_db_index1 where 1=1");
        if (ObjectUtils.isNotEmpty(kpExamScoringDbIndex1.getId())) {
            sql.append(" and id = '").append(kpExamScoringDbIndex1.getId()).append("' ");
        }
        if (ObjectUtils.isNotEmpty(kpExamScoringDbIndex1.getName())) {
            sql.append(" and name = '").append(kpExamScoringDbIndex1.getName()).append("' ");
        }
//        if (ObjectUtils.isNotEmpty(kpExamScoringDbIndex1.getScore())) {
//            sql.append(" and score = '").append(kpExamScoringDbIndex1.getScore()).append("' ");
//        }
        if (ObjectUtils.isNotEmpty(kpExamScoringDbIndex1.getKesdId())) {
            sql.append(" and kesd_id = '").append(kpExamScoringDbIndex1.getKesdId()).append("' ");
        }
        sql.append(" order by create_time asc");
        return jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(KpExamScoringDbIndex1.class));
    }

    public List<KpExamScoringDbIndex1> getByKrtytId(String krtytId) {
        return jdbcTemplate.query("SELECT t1.* from kp_exam_scoring_db_index1 t1 LEFT JOIN kp_exam_scoring_db t2 on t1.kesd_id = t2.id LEFT JOIN kp_rating_task t3 on t3.kesd_id = t2.id LEFT JOIN kp_rating_task_ypg_task t4 on t4.krt_id = t3.id where t4.id = '" + krtytId + "'", new BeanPropertyRowMapper<>(KpExamScoringDbIndex1.class));
    }
}
